package aaaAddr;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;

//import jcifs.smb.SmbFile;






import java.util.function.Function;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
//import org.slf4j.helpers.MessageFormatter;
import org.eclipse.jetty.webapp.WebAppContext;

import com.attilax.core;
import com.attilax.exception.ExUtil;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.Global;
//import com.attilax.net.urlEncode;
import com.attilax.web.UrlX;
import com.attilax.wrmi.WrmiServlet;

/**
 * jetty 9.3.7 aaaAddr.jetty4xssServer
 * 
 * @author Administrator
 *
 */
public class jetty4boxSys extends AbstractHandler {
	// private String smb;

	public int getPort() {
		return port;
	}

	public jetty4boxSys setPort(int port) {
		this.port = port;
		return this;
	}

	public int port;

	public void handle(String target, Request baseRequest,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		System.out.println("--taget:" + target); // /wrmiServlet zosh uri

		if (target.equals("/wrmiServlet"))
			new WrmiServlet().service(request, response);

		// response.setStatus(HttpServletResponse.SC_OK);
		baseRequest.setHandled(true);

	}

	/**
	 * http://127.0.0.1:7777/?smb=smb%3A%2F%2F192.168.2.106%2Fe%2F%E9%9D%
	 * 9E%E8%93%9D%E5%85%89%2F9%E8%B7%AF%E5%86%A5%E5%A9%9A%2F9%E8%B7%AF%E5%86%A5%E5%A9%9A-fanart.jp
	 * g http://127.0.0.1:7777/?smb=smb%3A%2F%2F192.168.2.106%2Fe%2F%E9%9D%
	 * 9E%E8%93%9D%E5%85%89%2F9%E8%B7%AF%E5%86%A5%E5%A9%9A%2F9%E8%B7%AF%E5%86%A5%E5%A9%9A.mk
	 * v
	 * 
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		System.out.println("");
		core.execMeth_Ays(new Runnable() {
			
			@Override
			public void run() {
				new jetty4boxSys()	.startX();
				
			}
		}, "jetty thread");
	
	}

	public   void startX(Function fun) 
	{
		
		System.setProperty("iocName", "aaaCfg.Ioc4timer");
		Global.iocCur = "aaaCfg.Ioc4timer";
		// HttpSessionIdListener
		// HttpSessionIdListener
		// MessageFormatter.arrayFormat(messagePattern, argArray)
		System.setProperty("prj", "jobus");// 指明是运行的哪个项目
		 // port = 8009;
		Server server = new Server(port);
		// ServletContextHandler
		WebAppContext context = new WebAppContext();
		// context.setcon
		context.setContextPath("/");
		try {
			Object resourceBase; // jetty4boxSys.class.getResource(resourceBase).toURI().toASCIIString()
			String webAppPath = pathx.webAppPath();
			// webAppPath=com.attilax.net.URLEncoder.encode(webAppPath);
			// URI uri=new URI(webAppPath);
			// String resBase = uri.toString();
			System.out.println(webAppPath);
			context.setResourceBase(webAppPath); // "c:\\0e"
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		context.addServlet(new ServletHolder(new WrmiServlet()), "/wrmiServlet");

		server.setHandler(context);
		// server.setHandler(new jetty4boxSys());

		try {
			server.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			ExUtil.throwEx(e);
		}
		System.out.println("--staered jetty9.3.7");
		fun.apply(null);
		System.out.println("--func apply" );
		try {
			server.join();
		} catch (InterruptedException e) {
			ExUtil.throwEx(e);
		}
		System.out.println("--f");
	
		
		
	}
	
	public   void startX()   {
		startX(new Function () {

			@Override
			public Object apply(Object t) {
				// TODO Auto-generated method stub
				return null;
			}
		});
	}

	// private jettyFileServer setSmb(String smb) {
	// this.smb=smb;
	// return this;
	// }
}